<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 *
 * 创建时间：2021-10-31 19:22:07
 *
 * 项目：/levfm  -  $  - AdminFieldsController.php
 *
 * 作者：{{AUTO GENERATE}}
 */

//此文件使用程序自动生成，下次生成时【不会】覆盖。
//在这里可以补充、完善你的程序逻辑

namespace modules\levfm\controllers;

use Lev;
use lev\base\Viewv;
use lev\helpers\UrlHelper;
use modules\levfm\controllers\_gen\BaseAdminFields;
use modules\levfm\helpers\fmUrlHelper;
use modules\levfm\table\form\Form_levfm_fields\Form_levfm_fields;
use modules\levfm\table\form\FormData\fieldValidations;
use modules\levfm\table\LevfmFields;
use modules\levfm\table\LevfmForms;

!defined('INLEV') && exit('Access Denied LEV');

class BaseAdminFieldsMiddle extends BaseAdminFields
{

    public static function actionIndex()
    {
        static::$addRoute .= '&formid='.floatval(Lev::GPv('formid'));
        return parent::actionIndex(); // TODO: Change the autogenerated stub
    }

    public static function headerHtm()
    {
        //return parent::headerHtm(); // TODO: Change the autogenerated stub
        //return '<tips class="gray inblk scale8">自定义headerHtm：文件位置：'.__DIR__ . '/AdminFieldsController.php'.'</tips>';
        $formid = floatval(Lev::GPv('formid'));
        list($inputs, $setInputs) = Form_levfm_fields::tabInputs();
        return Viewv::renderPartial('admin-fields/myself_index/headerHtmForm.php', [
            'inputs' => $inputs,
            'setInputs' => $setInputs,
            'inputsValues' => [],
            'formPre' => 'datax',
            'formid' => $formid,
        ]);
    }

    public static function footerHtm()
    {
        //return parent::footerHtm(); // TODO: Change the autogenerated stub
        //return '<tips class="gray inblk scale8">自定义footerHtm</tips>';

    }

    public static function cardFooterButtons($btns = [])
    {
        return parent::cardFooterButtons($btns); // TODO: Change the autogenerated stub
        //return '<tips class="gray inblk scale8">自定义cardFooterButtons</tips>';
    }

    public static function headerViewHtm()
    {
        return parent::headerViewHtm(); // TODO: Change the autogenerated stub
        //return '<tips class="gray inblk scale8">自定义headerViewHtm：文件位置：'.__DIR__ . '/AdminFieldsController.php'.'</tips>';
    }

    public static function footerViewHtm()
    {
        return parent::footerViewHtm(); // TODO: Change the autogenerated stub
        //return '<tips class="gray inblk scale8">自定义footerViewHtm</tips>';
    }

}

class AdminFieldsController extends BaseAdminFieldsMiddle
{

    public static $tmpPath = 'admin-fields';
    public static $trash   = true;//是否允许删除数据
    public static $addurl  = true;//是否显示创建数据按钮
    public static $copyOneBtn = false;
    public static $addRoute = 'Form_levfm_fields';//自定义创建数据表单地址 - 默认使用源表单

    public static $srhtitle    = '字段标题、类型';//模糊搜索字段名称
    public static $srhkeyWhere = "title LIKE '%{srhkey}%' OR inputtype LIKE '%{srhkey}%'";//模糊搜索sql语句 例：name LIKE '%{srhkey}%'

    public static $limit = 20;//每页显示多少条
    public static $order = ['id DESC'];//排序

    public static function col_inputtype() {
        return Form_levfm_fields::setinputtype();
    }

    public static function col_formid() {
        if (!isset(static::$tempDatas['formids'])) {
            static::$tempDatas['formids'] = [];
            $formids = Lev::getArrayColumn(static::$pages['lists'], ['formid']);
            if ($formids) {
                $result = LevfmForms::findAllField('id,name,settings', "id IN (".implode(',', $formids).")");
                foreach ($result as $v) {
                    static::$tempDatas['formids'][$v['id']] = $v['id'].'@<br>'.$v['name'];
                    $_fieldType = Lev::getSettings(Lev::getSettings($v['settings'], '_fieldType'));
                    if ($_fieldType) {
                        foreach ($_fieldType as $r) {
                            static::$tempDatas['classify'][$v['id']][$r['id']] = $r['id'] . '@' . $r['name'];
                        }
                    }
                }
            }
        }

        return static::$tempDatas['formids'];
    }

    public static function srhkeyUrl($srhkey, $value)
    {
        if ($srhkey == 'formid') {
            return Lev::toCurrent(['srhkey'=>null, 'isSrhkeyup'=>null, 'page'=>null, $srhkey=>$value, 'classify'=>null]);
        }
        return parent::srhkeyUrl($srhkey, $value); // TODO: Change the autogenerated stub
    }

    /**
     * 列表显示字段设置
     * 例：array(
    'id'           => array('order' => null, 'name' => 'Id', 'thattr' => 'wd30 nowrap', 'merge' => []),

    'name'         => array('order' => null, 'name' => '模块名称', 'thattr' => 'wd30 nowrap', 'merge' => ['addtime']),

    'displayorder' => array('order' => null, 'name' => '排序', 'thattr' => 'wd30 nowrap', 'merge' => []),

    'uptime'       => array('order' => null, 'name' => '更新时间', 'thattr' => 'wd30 nowrap', 'merge' => []),

    );
     *
     * order: 点击字段排序
     * name: 字段名称
     * thattr: 表格th属性，放class属性内，内容举例：wd30 nowrap" style="font-size:12px
     * merge: 合并显示，多个字段合并显示到一个上
     *
     * @see BaseAdminFields::showColumns();
     */
    public static function showColumns() {
        //return parent::showColumns(); // TODO: Change the autogenerated stub
        return array(
                'id'           => array('order' => null, 'name' => 'Id', 'thattr' => 'wd30 nowrap', 'merge' => ['inputname']),

                'formid'       => array('order' => null, 'name' => '关联表单ID', 'thattr' => 'wd30 nowrap', 'merge' => []),

                'classify'     => array('order' => null, 'name' => '字段分类', 'thattr' => 'wd30 nowrap', 'merge' => []),

                'title'        => array('order' => null, 'name' => '标题', 'thattr' => 'wd30 nowrap', 'merge' => []),

                //'inputname'    => array('order' => null, 'name' => '字段名', 'thattr' => 'wd30 nowrap', 'merge' => []),

                'inputtype'    => array('order' => null, 'name' => '字段类型', 'thattr' => 'wd30 nowrap', 'merge' => []),

                //'settings'     => array('order' => null, 'name' => '通用设置', 'thattr' => 'wd30 nowrap', 'merge' => []),

                'rules'        => array('order' => null, 'name' => '字段校验规则', 'thattr' => 'wd30 nowrap', 'merge' => []),

                'displayorder' => array('order' => 1, 'name' => '排序', 'thattr' => 'wd30 nowrap', 'merge' => []),

                'placeholder'  => array('order' => null, 'name' => '字段提示', 'thattr' => ' nowrap', 'merge' => ['inputvalue']),

                //'inputvalue'   => array('order' => null, 'name' => '字段默认值', 'thattr' => ' nowrap', 'merge' => []),

                'status'       => array('order' => null, 'name' => '状态', 'thattr' => 'wd30 nowrap', 'merge' => []),

                'uptime'       => array('order' => null, 'name' => '更新时间', 'thattr' => 'wd30 nowrap', 'merge' => []),

                //'addtime'      => array('order' => null, 'name' => '添加时间', 'thattr' => 'wd30 nowrap', 'merge' => []),

            );
    }

    /**
     *
     return [
        'showtype' => [
            //从上往下先到先得
            'input_{columnKey}'  => ['width'=>120,'textarea'=>1],//显示为无刷新更改字段
            'srhkey_{columnKey}' => ['or'=>['{columnKey}']],//显示为点击可搜索字段
            'status_{columnKey}' => 1,//显示为开关状态字段 status 自动
            'order_{columnKey}'  => 1,//显示为可排序输入框 含order关键字 自动
            'time_{columnKey}'   => 1,//显示为时间格式字段 含time关键字 自动
        ],
    ];
     *
     * @return array
     */
    public static function setColumnShowtype()
    {
        //return parent::setColumnShowtype(); // TODO: Change the autogenerated stub
        return [
            'showtype' => [
                //从上往下先到先得
                'input_classify'  => [],//['width'=>120,'textarea'=>1],//显示为无刷新更改字段
                'input_placeholder'  => ['width'=>'calc(100% - 10px) !important;','textarea'=>1],
                'input_inputvalue'  => ['width'=>'calc(100% - 10px) !important;','textarea'=>1],
                'input_inputtype'  => ['width'=>120],
                'input_title'  => ['width'=>120],
                'srhkey_formid' => [],//显示为点击可搜索字段
                'srhkey_classify' => [],//显示为点击可搜索字段
                'status_{columnKey}' => 1,//显示为开关状态字段 status 自动
                'order_{columnKey}'  => 1,//显示为可排序输入框 含order关键字 自动
                'time_{columnKey}'   => 1,//显示为时间格式字段 含time关键字 自动
            ],
        ];
    }

    public static function formatListsv($sv, $lists)
    {
        $v = $sv;
        $sv = parent::formatListsv($sv, $lists); // TODO: Change the autogenerated stub

        !isset(static::$tempDatas['formids']) && static::col_formid();

        $sv['classify'] = '<p class="date transl">'.static::srhkeyBtn(Lev::arrv([$v['formid'],$v['classify']], static::$tempDatas['classify'], $v['classify']), 'classify', $v['classify']).'</p>';
        if (isset(static::$tempDatas['classify'][$v['formid']])) {
            $select = '<p class="date transl"><select name="classify" class="wd120 set-field setField" opid="'.$v['id'].'" autocomplete="off">';
            foreach (static::$tempDatas['classify'][$v['formid']] as $kv => $cv) {
                $select .= '<option value="'.$kv.'" '.($kv == $v['classify'] ? 'selected' : '').'>' . $cv . '</option>';
            }
            $sv['classify'] .= $select.'</select></p>';
        }

        $rules = LevfmFields::getRules($sv['rules'], $sv['settings']);

        $rulestr = '<p style="max-width: 250px;"><absxy class="transl">'.fieldValidations::$dataTypes[$rules['dataType']].'</absxy></p>';
        $rulestr.= '<a class="link openPP" title="字段校验规则设置" data-reload="1" href="'.Lev::toReRoute(['Form_levfm_fields', 'setup'=>10, 'opid'=>$v['id'], 'id'=>'levfm', 'formid'=>$v['formid']]).'"><svg class="icon"><use xlink:href="#fa-set"></use></svg></a>';
        isset($rules['must']) && !$rules['must'] && $rulestr .= '<absx>必填</absx>';
        isset($rules['unique']) && !$rules['unique'] && $rulestr .= '<absxk>唯一</absxk>';
        !empty($rules['include']) && $rulestr .= '<absxg title="'.$rules['include'].'">过滤词</absxg>';
        !empty($rules['minLen']) && $rulestr .= '<absxn>最少'.$rules['minLen'].'</absxn>';
        !empty($rules['maxLen']) && $rulestr .= '<absxb>最多'.$rules['maxLen'].'</absxb>';
        $sv['rules'] = $rulestr;

        return $sv;
    }
    public static function srhkeyBtn($name, $key, $val) {
        return '<a href="'.static::srhkeyUrl($key, $val).'">'.$name.'</a>';
    }

    public static function formatUid($sv, $lists, $srhkey = ['uid'])
    {
        return parent::formatUid($sv, $lists, $srhkey); // TODO: Change the autogenerated stub
    }

    public static function redirct($columnKey, $sv)
    {
        return parent::redirct($columnKey, $sv); // TODO: Change the autogenerated stub
    }

    public static function optButtons($sv)
    {
        //return parent::optButtons($sv); // TODO: Change the autogenerated stub

        $btns[] = [
            'name' => '新增字段',
            'link' => fmUrlHelper::addField($sv['id']),
            'attr' => 'color-black'
        ];
        $btns[] = [
            'name' => '复制字段',
            'link' => static::copyOneUrl($sv['id']).'&doit=1',
            'attr' => 'color-yellow ajaxBtn" confirmmsg="您确定要复制【ID：'.$sv['id'].'】的整条数据吗？'
        ];

        $htms = '<div class="wd60 nowrap flex-box">';
        foreach ($btns as $v) {
            $htms .= '<a class="button button-fill wd100 wdmin '.$v['attr'].'" href="' . $v['link'] . '">'.$v['name'].'</a>';
        }
        return $htms.'</div>&nbsp;';
    }

    public static function footerBtns($btns = [])
    {
        return parent::footerBtns($btns); // TODO: Change the autogenerated stub
    }

}